/**
RoomBot, a IRC bot.
Copyright (C) 2009 Joshua Lückers

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.roombot.irc;

import com.googlecode.roombot.mysql.MySQLHandler;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * The IRC Conncetion Handler. This handler keeps a list of IRCConnections.
 * @author Joshua Lückers
 * @version 09-11-2009
 */
public class IRCConnectionHandler {
    private static IRCConnectionHandler connectionHandler;
    private ArrayList<IRCConnection> connections;

    static {
        connectionHandler = new IRCConnectionHandler();
    }

    /**
     * Initialize the ArrayList with IRCConnections.
     */
    public IRCConnectionHandler() {
        connections = new ArrayList<IRCConnection>();
        ResultSet servers = MySQLHandler.getInstance().selectQuery("SELECT * FROM servers");
        try {
            if (MySQLHandler.getInstance().isConnected()) {
                while (servers.next()) {
                    IRCConnection connection = new IRCConnection(servers.getString("server"), servers.getInt("port"),servers.getString("nickname"), servers.getString("altNickname"),servers.getString("passwd"));
                    Thread t = new Thread(connection);
                    t.start();
                    addIRCConnection(connection);
                    //addIRCConnection(new IRCConnection(servers.getString("server"), servers.getInt("port"),servers.getString("nickname"), servers.getString("altNickname"),servers.getString("passwd")));
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Add a new IRCConnection to the ArrayList.
     * @param connection The IRC connection to add
     */
    public void addIRCConnection(IRCConnection connection) {
        connections.add(connection);
//        connection.run();
    }

    /**
     * Remove a IRCConnection from the ArrayList.
     * @param connection The IRC connection to remove
     */
    public void removeIRCConnection(IRCConnection connection) {
        connections.remove(connection);
    }

    /**
     * Get all IRCConnections listed in the ArrayList
     * @return ArrayList<IRCConnection> A list of IRCConnections
     */
    public ArrayList<IRCConnection> getIRCConnections() {
        return connections;
    }

    /**
     * Get the IRCConenctionHandler instance.
     * @return IRCCOnnectionHandler The IRCConnectionHandler instance
     */
    public static IRCConnectionHandler getInstance() {
        return connectionHandler;
    }

}